<?php

function list_sisa_insentif(){
	my_set_code_js('
		function confirmDelete(sid){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&sid=\'+sid;
			}
			return false;
		}
	');

	$header = array(
		'Periode Insentif'=>array('style'=>'border-bottom:2px solid;width:40%'),   
		'Periode Gaji'=>array('style'=>'text-align:left;border-bottom:2px solid;width:50%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$tanggal = $periode['tanggal'];

	$query = "SELECT * FROM pr_sisa_insentif 
		ORDER BY sisa_insentif_id DESC";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while( $ey =my_fetch_array($result) ){
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_karyawan&sid='.$ey['sisa_insentif_id'],
			'title'=>'Komponen'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		if($ey['periode_bayar']>=$tanggal){
			$deleteproperty = array(
					'href'=>'javascript:; ',
					'onclick'=>'javascript:confirmDelete('.$ey['sisa_insentif_id'].');',
					'title'=>'Delete'
				);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}else{
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}
		$row[] = array(
			'tahun'=>$ey['periode_insentif'],
			'periode_bayar'=>date('m-Y',strtotime($ey['periode_bayar'])),
			' v'=>position_text_align($detail_button.' '.$delete_button,'right'),
		);
	}
	$datas = table_rows($row);
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_sisa_insentif&task=create\' });'),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  3 , false , $paging);
}

function form_sisa_insentif(){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "a" , "a"  );
	$month = date('m');
	$year = date('Y');

	$temp = array(
		'name'=>'periode_insentif',
		'size'=>5,
		'value'=>isset($_POST['periode_insentif']) ? $_POST['periode_insentif']: $year-1
	); 
	$temp_form = form_dynamic($temp );
	$view .= form_field_display( $temp_form  , "Periode Insentif"    );
	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );
	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode Bayar"    );

	$submit = array(
		'value' => ' Simpan ' ,
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function sisa_insentif_validate(){
	$errsubmit = false;
	$err = array();
	if(!is_numeric($_POST['periode_insentif']) ){
		$errsubmit = true;
		$err[] = "Periode Insentif harus angka" ;
	}
	if(!is_numeric($_POST['tahun']) ){
		$errsubmit = true;
		$err[] = "Tahun Periode Bayar harus angka" ;
	}	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function submit_sisa_insentif(){
	$tanggal = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$datas = array(
		'periode_insentif'=> my_type_data_int($_POST['periode_insentif']),
		'periode_bayar'=> my_type_data_str($tanggal),	
	);
	
	$sid=my_insert_record( 'pr_sisa_insentif' , $datas );
	return update_sisa_insentif_karyawan($sid) ;
	
}
 
function delete_sisa_insentif($sid){
	$query = "DELETE FROM pr_sisa_insentif WHERE sisa_insentif_id={$sid}";
	my_query($query);		
	$query = "DELETE FROM pr_sisa_insentif_karyawan WHERE sisa_insentif_id={$sid}";
	my_query($query);	
}	

function update_sisa_insentif_karyawan($sid){

	ini_set("max_execution_time","10000");	
	$query = "DELETE FROM pr_sisa_insentif_karyawan
		WHERE sisa_insentif_id={$sid}";
	$result = my_query($query); //reset				

	$pr_sisa_insentifs=my_get_data_by_id('pr_sisa_insentif','sisa_insentif_id',$sid) ;
	
	$tahun_insentif=$pr_sisa_insentifs['periode_insentif'] ;
	$query = "
			SELECT * FROM pr_insentif_karyawan a 
			LEFT JOIN pr_insentif b ON b.insentif_id=a.insentif_id 
			WHERE a.insentif_id IN 
			(SELECT insentif_id FROM pr_insentif WHERE year(tanggal)={$tahun_insentif})
			ORDER BY a.karyawan_id
		";	
	$result = my_query($query);
	$karyawan_id=0;	
	$bln[1]=$bln[2]=$bln[3]=$bln[4]=$bln[5]=$bln[6]=$bln[7]=$bln[8]=$bln[9]=$bln[10]=$bln[11]=$bln[12]=$total=0;
	while($ey = my_fetch_array($result)){
		if($karyawan_id<>$ey['karyawan_id']){
			if($karyawan_id<>0){
				
				$datas = array(
					'sisa_insentif_id'=>my_type_data_int($sid),
					'karyawan_id'=>my_type_data_int($karyawan_id),
					'bln_01'=>my_type_data_str($bln[1]),
					'bln_02'=>my_type_data_str($bln[2]),
					'bln_03'=>my_type_data_str($bln[3]),
					'bln_04'=>my_type_data_str($bln[4]),
					'bln_05'=>my_type_data_str($bln[5]),
					'bln_06'=>my_type_data_str($bln[6]),
					'bln_07'=>my_type_data_str($bln[7]),
					'bln_08'=>my_type_data_str($bln[8]),
					'bln_09'=>my_type_data_str($bln[9]),
					'bln_10'=>my_type_data_str($bln[10]),
					'bln_11'=>my_type_data_str($bln[11]),
					'bln_12'=>my_type_data_str($bln[12]),
					'total'=>my_type_data_str($total),
				);
				my_insert_record('pr_sisa_insentif_karyawan',$datas);
			}
			$karyawan_id=$ey['karyawan_id'];
			$bln[1]=$bln[2]=$bln[3]=$bln[4]=$bln[5]=$bln[6]=$bln[7]=$bln[8]=$bln[9]=$bln[10]=$bln[11]=$bln[12]=$total=0;
		}
		$mm = (int) date('m',strtotime($ey['tanggal']));
		$bln[ $mm ]= $ey['nominal']	;
		$total+= (int) $ey['nominal'];
	}
	if($karyawan_id<>0){
		$datas = array(
			'sisa_insentif_id'=>my_type_data_int($sid),
			'karyawan_id'=>my_type_data_int($karyawan_id),
			'bln_01'=>my_type_data_str($bln[1]),
			'bln_02'=>my_type_data_str($bln[2]),
			'bln_03'=>my_type_data_str($bln[3]),
			'bln_04'=>my_type_data_str($bln[4]),
			'bln_05'=>my_type_data_str($bln[5]),
			'bln_06'=>my_type_data_str($bln[6]),
			'bln_07'=>my_type_data_str($bln[7]),
			'bln_08'=>my_type_data_str($bln[8]),
			'bln_09'=>my_type_data_str($bln[9]),
			'bln_10'=>my_type_data_str($bln[10]),
			'bln_11'=>my_type_data_str($bln[11]),
			'bln_12'=>my_type_data_str($bln[12]),
			'total'=>my_type_data_str($total),
		);
		my_insert_record('pr_sisa_insentif_karyawan',$datas);
	}
	return true;	
}

function list_karyawan($sid){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:50%'),  
		'Total Sisa Insentif'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 		
	);
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM pr_sisa_insentif_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			INNER JOIN biodata c ON c.biodata_id=b.biodata_id 
			WHERE a.sisa_insentif_id={$sid} 
			AND ( c.nama LIKE '%{$_GET['key']}%' OR b.nik ='{$_GET['key']}' )
			ORDER BY b.nik 
		";
	}else{
		$query = "
			SELECT * FROM pr_sisa_insentif_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			WHERE a.sisa_insentif_id={$sid} 
			ORDER BY b.nik 
		";
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&sid={$_GET['sid']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25;  
	while($ey = my_fetch_array($result)){
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_bulan&sid='.$sid.'&karyawan_id='.$ey['karyawan_id'],
			'title'=>'Detail per Bulan'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
 		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,   
			'nama'=>  $karyawan['nama_gelar'] ,   
			'total'=> position_text_align (rp_format($ey['total']),'right')  ,   
			'operasi'=> position_text_align( $detail_button, 'right'),
		);
	}
	$search = '<form method="GET">
	<input type="hidden" name="com" value="'.$_GET['com'].'" />
	<input type="hidden" name="task" value="'.$_GET['task'].'" />
	<input type="hidden" name="sid" value="'.$_GET['sid'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$sisa_insentifs = my_get_data_by_id('pr_sisa_insentif','sisa_insentif_id',$sid);
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= '<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= '<tr><td>'.$search .'</td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.  $sisa_insentifs['periode_insentif'] .'</span>'   , "<b>Periode Insentif</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($sisa_insentifs['periode_bayar'])) .'</span>'   , "<b>Periode Bayar</b>"    ); 
	$view .= '</table>';
	$view .= '</td></tr></table><br/>';
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak_det&sid='.$sid.'&key='.$_GET['key'].'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_det&sid='.$sid.'&key='.$_GET['key'].'\';'),
		'Kembali'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\';'),
	);	
	$printmenu = additional_menu_on_list( $menuontop ,'' ); 	
	$datas = table_rows($row);   
	$paging = $kgPagerOBJ ->showPaging();
	return  $view .$printmenu.  table_builder($header , $datas , 4, false ,$paging);
}

function list_bulan($sid,$karyawan_id){
	
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Bulan'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Sisa Insentif'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:65%'), 		
	);
	$query = "
		SELECT * FROM pr_sisa_insentif_karyawan 
		WHERE sisa_insentif_id={$sid} 
		AND karyawan_id={$karyawan_id} 
		LIMIT 1 
	";
	$result = my_query($query );
	$sisa_insentifs = my_fetch_array($result);
	$row = array(); 
	for ($i=1; $i<=12; $i++){
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'bln'=> returnDate( $i, "month") ,
			'total'=> position_text_align (rp_format($sisa_insentifs['bln_'.sprintf('%02d',$i)]),'right')  ,  
			'ops'=> ' ',   			
		);
	}
	$row[] = array(
		'#'=>' ', 
		'bln'=> ' T o t a l' ,
		'total'=> position_text_align (rp_format($sisa_insentifs['total']),'right')  ,  
		'ops'=> ' ',
	);
	$sisa_insentifs = my_get_data_by_id('pr_sisa_insentif','sisa_insentif_id',$sid);

	$karyawan = loaddata_karyawan($karyawan_id); 
	$newline = array(
		'Periode Insentif'=>$sisa_insentifs['periode_insentif'],
		'Periode Bayar'=>date( "m-Y" , strtotime($sisa_insentifs['periode_bayar'])),
	);
	$view = karyawan_header_detail($karyawan  ,  true , $newline );

	$menuontop = array(  
		'Kembali'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=list_karyawan&sid='.$sid.'\';'),
	);	
	$printmenu = additional_menu_on_list( $menuontop ,'' ); 	
	$datas = table_rows($row);   
	return  $view .$printmenu.  table_builder($header , $datas , 4, false );
}


function excel_report_det($sid){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Januari'=>array('style'=>'border-bottom:2px solid;width:10%'),   
		'Februari'=>array('style'=>'border-bottom:2px solid;width:10%'),   
		'Maret'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'April'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Mei'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Juni'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Juli'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Agustus'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'September'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Oktober'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'November'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Desember'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'Total'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
	);
	
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM pr_sisa_insentif_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			INNER JOIN biodata c ON c.biodata_id=b.biodata_id 
			WHERE a.sisa_insentif_id={$sid} 
			AND ( c.nama LIKE '%{$_GET['key']}%' OR b.nik ='{$_GET['key']}' )
			ORDER BY b.nik 
		";
	}else{
		$query = "
			SELECT * FROM pr_sisa_insentif_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			WHERE a.sisa_insentif_id={$sid} 
			ORDER BY b.nik 
		";
	}
	$result = my_query($query );

	$row = array(); 
	$i = 0;  
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
 		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,   
			'nama'=>  $karyawan['nama_gelar'] ,   
			'bln_01'=> position_text_align ($ey['bln_01'],'right')  ,   
			'bln_02'=> position_text_align ($ey['bln_02'],'right')  ,   
			'bln_03'=> position_text_align ($ey['bln_03'],'right')  ,   
			'bln_04'=> position_text_align ($ey['bln_04'],'right')  ,   
			'bln_05'=> position_text_align ($ey['bln_05'],'right')  ,   
			'bln_06'=> position_text_align ($ey['bln_06'],'right')  ,   
			'bln_07'=> position_text_align ($ey['bln_07'],'right')  ,   
			'bln_08'=> position_text_align ($ey['bln_08'],'right')  ,   
			'bln_09'=> position_text_align ($ey['bln_09'],'right')  ,   
			'bln_10'=> position_text_align ($ey['bln_10'],'right')  ,   
			'bln_11'=> position_text_align ($ey['bln_11'],'right')  ,   
			'bln_12'=> position_text_align ($ey['bln_12'],'right')  ,   
			'total'=> position_text_align ($ey['total'],'right')  ,   
		);
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}

function print_plain_det($sid){

	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM pr_sisa_insentif_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			INNER JOIN biodata c ON c.biodata_id=b.biodata_id 
			WHERE a.sisa_insentif_id={$sid} 
			AND ( c.nama LIKE '%{$_GET['key']}%' OR b.nik ='{$_GET['key']}' )
			ORDER BY b.nik 
		";
	}else{
		$query = "
			SELECT * FROM pr_sisa_insentif_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id=b.karyawan_id 
			WHERE a.sisa_insentif_id={$sid} 
			ORDER BY b.nik 
		";
	}
	$result = my_query($query );

	$datas=array();
	$datas[] = array(
		 'k1'=>array("#" ,5 ,STR_PAD_BOTH),
		 'k2'=>array("NIK" ,10,STR_PAD_RIGHT),
		 'k3'=>array("Nama Karyawan" ,50,STR_PAD_RIGHT),
		 'k4'=>array("Total Sisa Insentif" ,30,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_BOTH),
		 'k2'=>array(" " ,10,STR_PAD_RIGHT),
		 'k3'=>array(" " ,50,STR_PAD_RIGHT),
		 'k4'=>array(" " ,30,STR_PAD_LEFT),  
	); 

	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$datas[] = array(
			'k1'=>array($i ,5 ,STR_PAD_BOTH),
			'k2'=>array($karyawan['nik'] ,10,STR_PAD_RIGHT),
			'k3'=>array($karyawan['nama_gelar'] ,50,STR_PAD_RIGHT),
			'k4'=>array(rp_format($ey['total']) ,30,STR_PAD_LEFT),  
		); 
	
	}
	$view = 	str_pad("CETAK SISA INSENTIF",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}
